// -----------------------------------------------------------------------
//  <copyright file="GetUserByIdQuery.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/10 9:16:05</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Identity.Domain.Entities;


namespace DaprPlus.Identity.Application.Identity.Users.Queries;

[AuthConfig(typeof(User), ResourceAccessType.RoleLimit, "GetById")]
public class GetUserByIdQuery : SingleQuery<UserOutDto?>
{
    public GetUserByIdQuery(long id)
    {
        Check.GreaterThan(id, nameof(id), 0);
        Id = id;
    }

    public long Id { get; set; }
}

public class GetUserByIdQueryHandler(IQueryRepository<User> queryRepository) : IRequestHandler<GetUserByIdQuery, UserOutDto?>
{
    public async Task<UserOutDto?> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
    {
        var spec = new UserByIdSpec<User>(request.Id);
        var model = await queryRepository.GetAsync(spec, cancellationToken);
        return model?.MapTo<UserOutDto?>();
    }
}

